<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class StatController extends Controller
{
    /*统计页面*/
    public function stat(){

        // 前台用户数  $h_user_count
        $h_user_count = DB::table('home_user') -> count();
        
        // 今日注册用户数 $h_user_count_today
        $h_user_count_today = DB::table('home_user') -> where('createTime','>=',date('Y-m-d',time()) ) -> count();
        
        // 后台管理员数 $a_user_count
        $a_user_count = DB::table('admin_user') -> count();
        
        // 商品数量 $goods_count
        $goods_count = DB::table('goods') -> count();
        
        // 上线商品数量 1上线 2下线  $goods_count_up
        $goods_count_up = DB::table('goods') -> where('gstatus',1) -> count();
        
        // 今日增加商品数量 $goods_count_today
        $goods_count_today = DB::table('goods') -> where('createTime','>=',date('Y-m-d',time()) ) -> count();
       
        // 今日总销售金额  $total_today
        $total_today = 0;
        $date = DB::table('order') -> where('otime','>=',strtotime(date('Y-m-d',time()))) -> get();
        foreach($date as $k => $v){
            $total_today += $v['oprice'];
        }
        
        // 今日类别销售金额 $type_today
        $type_today = DB::table('cate') -> where('pid',0) -> select('cid','cname') -> get();
        
        $order_today = DB::table('order_detail') 
                       -> join('goods','order_detail.gid','=','goods.gid') 
                       -> join('order','order_detail.oid','=','order.oid') 
                       -> where('otime','>=',strtotime(date('Y-m-d',time()))) 
                       -> get();
      
        foreach($type_today as $kk => $vv){
            $type_today[$kk]['total'] = 0;
            foreach($order_today as $k => $v){ 
                // 需判断是否存在要查询的类
                if(!empty(DB::table('cate') -> where('cid',$v['cid']) -> first()['path'])){
                    // 在类别表中查询order中的cid,如果结果中的path的第二个类别等于type的类别,则可得知详情表的商品属于本大类
                    if( explode(',',DB::table('cate') -> where('cid',$v['cid']) -> first()['path'])[1] == $vv['cid'] ){
                        // 前面order_detail的gprice被goods表的gprice覆盖,在这里再重新赋值
                        $v['gprice'] = DB::table('order_detail') -> where('did',$v['did']) -> first()['gprice'];
                        $type_today[$kk]['total'] += $v['gprice'] * $v['gnum'];
                    }
                }
            }
        }       

        // 历史类别销售金额 $type_his
        $type_his = DB::table('cate') -> where('pid',0) -> select('cid','cname') -> get();
        
        $order_his = DB::table('order_detail') 
                        -> join('goods','order_detail.gid','=','goods.gid') 
                        -> get();

        foreach($type_his as $kk => $vv){                
            $type_his[$kk]['total'] = 0;
            foreach($order_his as $k => $v){
                // 需判断是否存在要查询的类
                if(!empty(DB::table('cate') -> where('cid',$v['cid']) -> first()['path'])){
                    // 在类别表中查询order中的cid,如果结果中的path的第二个类别等于type的类别,则可得知详情表的商品属于本大类
                    if( explode(',',DB::table('cate') -> where('cid',$v['cid']) -> first()['path'])[1] == $vv['cid'] ){
                        // 前面order_detail的gprice被goods表的gprice覆盖,在这里再重新赋值
                        $v['gprice'] = DB::table('order_detail') -> where('did',$v['did']) -> first()['gprice'];
                        $type_his[$kk]['total'] += ($v['gprice'] * $v['gnum']);
                    }
                }
            }
        }   
        
        // 历史总销售金额 $total
        $total = 0;
        $date = DB::table('order') -> get();
        foreach($date as $k => $v){
            $total += $v['oprice'];
        }
        
        // 评论总数 $comment_count
        $comment_count = DB::table('comment') -> count();
        
        // 今日新增评论数 $comment_count_today
        $comment_count_today = DB::table('comment') -> where('time','>',strtotime(date('Y-m-d',time()))) -> count();

        return view('/admin/stat/stat',[
            'h_user_count' => $h_user_count,  //前台用户数
            'h_user_count_today' => $h_user_count_today, //今日注册用户数
            'a_user_count' => $a_user_count, //后台管理员数
            'goods_count' => $goods_count, //商品数量
            'goods_count_up' => $goods_count_up, //上线商品数量
            'goods_count_today' => $goods_count_today, //今日增加商品数量
            'total_today' => $total_today,  //今日总销售金额
            'type_today' => $type_today, //今日类别销售金额
            'type_his' => $type_his, //历史类别销售金额
            'total' => $total, //历史总销售金额
            'comment_count' => $comment_count, //评论总数
            'comment_count_today' => $comment_count_today //今日新增评论数
        ]);
    }


    /*获得历史单日记录*/
    public function dd(Request $request){
        // 获得日期
        $date = $request -> input('date');
        // 获得一天的秒数
        $oneday = 24*60*60;
        // 获得日期的时间戳
        $date = strtotime($date);

        // 获得所有的大类
        $type = DB::table('cate') -> where('pid',0) -> select('cid','cname') -> get();
        
        // 获取指定日期的订单详情
        $order = DB::table('order_detail') 
                       -> join('goods','order_detail.gid','=','goods.gid') 
                       -> join('order','order_detail.oid','=','order.oid') 
                       -> where('otime','>=',$date) 
                       -> where('otime','<',$date+$oneday) 
                       -> get();
        // 
        foreach($type as $kk => $vv){
            $type[$kk]['total'] = 0;
            foreach($order as $k => $v){ 
                // 需判断是否存在要查询的类
                if(!empty(DB::table('cate') -> where('cid',$v['cid']) -> first()['path'])){
                    // 在类别表中查询order中的cid,如果结果中的path的第二个类别等于type的类别,则可得知详情表的商品属于本大类
                    if( explode(',',DB::table('cate') -> where('cid',$v['cid']) -> first()['path'])[1] == $vv['cid'] ){
                        // 前面order_detail的gprice被goods表的gprice覆盖,在这里再重新赋值
                        $v['gprice'] = DB::table('order_detail') -> where('did',$v['did']) -> first()['gprice'];
                        $type[$kk]['total'] += $v['gprice'] * $v['gnum'];
                    }
                }
            }
        }
        // json编码
        $type = json_encode($type);

        echo $type;
    }

}
